<template>
  <el-dialog
    :visible.sync="visible"
    title="服务信息录入"
    width="60%"
    :before-close="handleClose"
  >
    <el-form
      :model="startServiceForm"
      :rules="startServiceFromRules"
      ref="startServiceFromRef"
      label-width="110px"
    >
      <el-form-item label="开始服务时间" prop="real_start_time">
        <el-date-picker
          v-model="startServiceForm.real_start_time"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetime"
          placeholder="选择日期时间"
          style="width: 100%"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="服务凭证码" prop="evidences_code">
        <el-input
          v-model="startServiceForm.evidences_code"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button class="theme-background-color" @click="saveFormData"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    startServiceDialogVisible: {
      type: Boolean,
      default: function () {
        return {}
      }
    },
    orderServiceId: {
      type: Number,
      default: function () {
        return {}
      }
    }
  },
  data() {
    return {
      visible: false,
      startServiceForm: {
        real_start_time: '',
        evidences_code: ''
      },
      startServiceFromRules: {
        real_start_time: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        evidences_code: [
          { required: true, message: '请输入', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          this.$emit('update:startServiceDialogVisible', false)
          done()
        })
        .catch((_) => {})
    },
    closeDialog() {
      this.$emit('update:startServiceDialogVisible', false)
    },
    saveFormData() {
      this.$refs.startServiceFromRef.validate(async (valid) => {
        if (!valid) return
        this.startServiceForm.order_id = this.orderServiceId
        // console.log(this.startServiceForm)

        const { data: res } = await this.$http.post(
          'admin/OrderBooking/starSer',
          this.startServiceForm
        )
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }
        this.$message.success('服务信息录入成功')
        this.visible = false
        this.$emit('update:startServiceDialogVisible', false)
        this.$parent.getDetailData()
      })
    }
  },
  watch: {
    startServiceDialogVisible() {
      this.visible = this.startServiceDialogVisible
      if (!this.visible) {
        this.$refs.startServiceFromRef.resetFields()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 80%;
  margin-left: 15px;
  .el-select {
    width: 100%;
  }
}
</style>
